package com.ruoyi.project.system.register.controller;

import java.util.List;

import cn.hutool.core.util.IdUtil;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.security.ShiroUtils;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.project.system.doctor.domain.TableDoctor;
import com.ruoyi.project.system.doctor.service.ITableDoctorService;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.system.register.domain.TableRegister;
import com.ruoyi.project.system.register.service.ITableRegisterService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 挂号信息Controller
 * 
 * @author ruoyi
 * @date 2023-09-13
 */
@Controller
@RequestMapping("/system/register")
public class TableRegisterController extends BaseController
{
    private String prefix = "system/register";
    @Autowired
    private ITableDoctorService tableDoctorService;
    @Autowired
    private ITableRegisterService tableRegisterService;

    @RequiresRoles(value = {"manager","patient"},logical =  Logical.OR)
    @GetMapping()
    public String register()
    {
        return prefix + "/register";
    }

    /**
     * 查询挂号信息列表
     */
    @RequiresRoles(value = {"manager","patient"},logical =  Logical.OR)
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(TableRegister tableRegister)
    {
        boolean admin = ShiroUtils.getSysUser().isManager();
        if(!admin){
            tableRegister.setName(ShiroUtils.getSysUser().getUserName());
        }
        startPage();
        List<TableRegister> list = tableRegisterService.selectTableRegisterList(tableRegister);
        return getDataTable(list);
    }

    /**
     * 导出挂号信息列表
     */
    @RequiresRoles(value = {"manager","patient"},logical =  Logical.OR)
    @Log(title = "挂号信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(TableRegister tableRegister)
    {
        List<TableRegister> list = tableRegisterService.selectTableRegisterList(tableRegister);
        ExcelUtil<TableRegister> util = new ExcelUtil<TableRegister>(TableRegister.class);
        return util.exportExcel(list, "挂号信息数据");
    }

    /**
     * 新增挂号信息
     */
    @RequiresRoles(value = {"patient"})
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存挂号信息
     */
    @RequiresRoles(value = {"patient"})
    @Log(title = "挂号信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(TableRegister tableRegister)
    {
        TableDoctor query = new TableDoctor();
        query.setName(tableRegister.getDoctorName());
        TableDoctor tableDoctor = tableDoctorService.selectTableDoctorList(query).get(0);

        tableRegister.setSuoying(IdUtils.fastSimpleUUID());
        tableRegister.setYuyueNum(DateUtils.getDate().replace("-","")+ IdUtil.fastSimpleUUID().substring(0,6));
        tableRegister.setDoctorNum(tableDoctor.getGonghao());
        tableRegister.setDoctorZhicheg(tableDoctor.getZhicheng());
        tableRegister.setKeshiName(tableDoctor.getKeshi());
        tableRegister.setJiuzhengnStatus("1");
        tableRegister.setUserName(ShiroUtils.getLoginName());
        tableRegister.setName(ShiroUtils.getSysUser().getUserName());
        tableRegister.setGuahaoTime(DateUtils.getNowDate());
        return toAjax(tableRegisterService.insertTableRegister(tableRegister));
    }

    /**
     * 修改挂号信息
     */
    @RequiresRoles(value = {"manager","patient"},logical =  Logical.OR)
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        TableRegister tableRegister = tableRegisterService.selectTableRegisterById(id);
        mmap.put("tableRegister", tableRegister);
        return prefix + "/edit";
    }

    /**
     * 修改保存挂号信息
     */
    @RequiresRoles(value = {"manager","patient"},logical =  Logical.OR)
    @Log(title = "挂号信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(TableRegister tableRegister)
    {
        tableRegister.setShengheStatus("2");
        return toAjax(tableRegisterService.updateTableRegister(tableRegister));
    }

    /**
     * 删除挂号信息
     */
    @RequiresRoles(value = {"manager","patient"},logical =  Logical.OR)
    @Log(title = "挂号信息", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(tableRegisterService.deleteTableRegisterByIds(ids));
    }
}
